package gui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;

import client.ClientConsole;

import logic.LendReport;
import logic.Message;

/** 
 * produces and shows the lends report
 * shows statistic information for wanted and regular books
 */
public class LendsReportWindow extends JPanel implements ActionListener{
	/**  GUI Panels used for Design*/
	private JPanel mainPanel,reportPanel,backbottom,topPanel;
	/**error label show the feedback from server*/
	private JLabel wantedAvgLabel,wantedMedianLabel,regularAvgLabel,regularmedianLabel,wantedAvg,wantedMedian,
	regularAvg,regularmedian,dateLabel,error;
	private JButton back;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	
	/**Default constructor-Generates and create the lends report*/
	public LendsReportWindow()
	{
		super();
		setSize(300, 300);
		setLayout(new BorderLayout());
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		topPanel=new JPanel();
		topPanel.setLayout(new BorderLayout());
		dateLabel=new JLabel();
		topPanel.add(dateLabel,BorderLayout.WEST);
		mainPanel.add(topPanel,BorderLayout.NORTH);
		reportPanel=new JPanel();
		reportPanel.setLayout(new GridBagLayout());
		gbc.insets= new Insets(7,7,7,7);
		wantedAvgLabel=new JLabel("Average of wanted books: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(wantedAvgLabel,gbc);
		wantedAvg=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(wantedAvg,gbc);
		wantedMedianLabel=new JLabel("Median of wanted books: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(wantedMedianLabel,gbc);
		wantedMedian=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(wantedMedian,gbc);
		regularAvgLabel=new JLabel("Average of regular books: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=2;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(regularAvgLabel,gbc);
		regularAvg=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(regularAvg,gbc);
		regularmedianLabel=new JLabel("Median of regular books: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=3;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(regularmedianLabel,gbc);
		regularmedian=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=3;
		gbc.insets= new Insets(4,4,4,4);
		reportPanel.add(regularmedian,gbc);
		mainPanel.add(reportPanel,BorderLayout.CENTER);
		
		back=new JButton("back");
		backbottom=new JPanel();
		backbottom.add(back);
		error=new JLabel();
		error.setVisible(false);
		backbottom.add(error);
		back.addActionListener(this);
		mainPanel.add(backbottom,BorderLayout.SOUTH);
		add(mainPanel,BorderLayout.CENTER);
		produceReport();
	
	}
	
	/**
	 * sends a request to generate new Report and show its properties
	 */
	private void produceReport() {
		Message msg=new Message ("Get Lend Report",null);
		ClientConsole.HandleMessageFromGui(msg);
		msg=(Message)ClientConsole.client.getMessage();
		if (msg.getCommand().equals("Returned Lend Report"))
		{
			LendReport lReport=(LendReport) msg.getObjects();
			wantedAvg.setText(String.valueOf(lReport.getWantedAvg())+" Days");
			wantedMedian.setText(String.valueOf(lReport.getWantedMedian())+" Days");
			regularAvg.setText(String.valueOf(lReport.getRegularAvg())+" Days");
			regularmedian.setText(String.valueOf(lReport.getRegularMedian())+" Days");
			dateLabel.setText(lReport.getReportDate().toString());
		}
		else
		{
			error.setText(msg.getCommand());
		}
	}
	
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==back)
		{
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new ProduceReportWindow());
		}
		
	}

}
